import { request } from "../../utils/request.js";
let app = getApp()

Page({

    data: {
        write_kind_list: [{
            word: '支出',
            choose: true,
            type: 0
        },
        {
            word: '入账',
            choose: false,
            type: 1
        }
        ],
        categroyNameForShow: "",
        categroyWarnForShow: null,
        categoryList: [],
        categoryIdByChoose: 0,
        updateId: 0,
        configColor: app.globalData.configColor,
        configColorForOnlyStyle: app.globalData.configColorForOnlyStyle,
        statusBarHeight: app.globalData.statusBarHeight,
        navBarHeight: app.globalData.navBarHeight,
    },

    go_category() {
        wx.redirectTo({
            // url: '/pages/category/category?isOut=' + this.data.write_kind_list[0].choose,
            url: '/pages/category/category?isOut=' + this.data.write_kind_list[0].choose + '&chooseGroupId=' + this.data.chooseGroupId + '&chooseGroupName=' + this.data.chooseGroupName
        });
    },
    // 选择支出或者入账
    choose_type(e) {
        let write_kind_list = this.data.write_kind_list
        write_kind_list.forEach((item) => {
            item.choose = false
        })
        let index = e.currentTarget.dataset.index * 1
        write_kind_list[index].choose = true
        this.setData({
            write_kind_list
        })
    },
    choose_kinds(e) {
        let categoryList = this.data.categoryList
        categoryList.forEach((item) => {
            item.categoryList.forEach((item) => {
                item.choose = false
            })
        })
        let index = e.currentTarget.dataset.index * 1
        let childrenindex = e.currentTarget.dataset.childrenindex * 1
        categoryList[index].categoryList[childrenindex].choose = true
        this.setData({
            categoryList: categoryList,
            categoryIdByChoose: categoryList[index].categoryList[childrenindex].id
        })
    },
    inputCategoryName(e) {
        this.setData({
            categroyNameForShow: e.detail
        })
    },
    inputCategoryWarn(e) {
        this.setData({
            categroyWarnForShow: e.detail > 0 ? e.detail : null
        })
    },
    saveCategory() {
        if (!this.data.categroyNameForShow) {
            wx.showToast({
                title: '请输入名称',
                icon: 'none'
            })
            return
        }
        if (this.data.categoryIdByChoose < 1) {
            wx.showToast({
                title: '请选择图标',
                icon: 'none'
            })
            return
        }
        let isOut = false
        if (this.data.write_kind_list[0].choose) {
            isOut = true
        }
        wx.request({
            url: app.globalData.url + '/operateCategory',
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            data: {
                userId: wx.getStorageSync('userInfo').id,
                id: this.data.updateId,
                isOut: isOut,
                categoryName: this.data.categroyNameForShow,
                warningNum: this.data.categroyWarnForShow ? this.data.categroyWarnForShow : 0,
                categoryId: this.data.categoryIdByChoose,
                groupId: this.data.chooseGroupId
            },
            success: (res) => {
                let responseData = res.data
                if (responseData.flag) {
                    wx.showToast({
                        title: '操作成功',
                        icon: 'success'
                    })
                    wx.redirectTo({
                        url: '/pages/category/category?isOut=' + this.data.write_kind_list[0].choose + '&chooseGroupId=' + this.data.chooseGroupId + '&chooseGroupName=' + this.data.chooseGroupName
                    });
                } else {
                    wx.showToast({
                        title: '操作失败:' + responseData.message,
                        icon: 'none'
                    })
                }
            },
            fail: (err) => {
                console.log('失败', err);
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        request({   //调用请求方法
            url: "/getAllCategory",
            method: "GET"
        }).then((responseData) => {
            this.setData({
                chooseGroupId: options.chooseGroupId,
                chooseGroupName: options.chooseGroupName
            })
            if (options.row) {
                // 修改
                let rowData = JSON.parse(options.row)
                let write_kind_list = this.data.write_kind_list
                write_kind_list.forEach((item) => {
                    item.choose = false
                })
                if (rowData.isOut) {
                    write_kind_list[0].choose = true
                } else {
                    write_kind_list[1].choose = true
                }
                responseData.forEach((item) => {
                    item.categoryList.forEach((item1) => {
                        if (item1.id == rowData.categoryId) {
                            item1.choose = true
                            return
                        }
                    })
                })
                this.setData({
                    updateId: rowData.id,
                    categoryList: responseData,
                    categroyNameForShow: rowData.categoryName,
                    categroyWarnForShow: rowData.warningNum > 0 ? rowData.warningNum : null,
                    categoryIdByChoose: rowData.categoryId,
                    write_kind_list
                })
            } else {
                // 新增
                let write_kind_list = this.data.write_kind_list
                write_kind_list.forEach((item) => {
                    item.choose = false
                })
                if (options.isOut == "true" || options.isOut == 1) {
                    write_kind_list[0].choose = true
                } else {
                    write_kind_list[1].choose = true
                }
                this.setData({
                    write_kind_list,
                    categoryList: responseData
                })
            }
        })


    },
    onShow: function () {
        // 切换主题颜色
        if (wx.getStorageSync('configColor') !== this.data.configColor
            || wx.getStorageSync('configColorForOnlyStyle') !== this.data.configColorForOnlyStyle) {
            this.setData({ configColor: wx.getStorageSync('configColor') });
            this.setData({ configColorForOnlyStyle: wx.getStorageSync('configColorForOnlyStyle') });
        }
    }

})